package ynu.edu.merchant.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.result.Result;
import ynu.edu.merchant.dto.MerchantCategoryDTO;
import ynu.edu.merchant.service.MerchantCategoryService;
import ynu.edu.merchant.vo.MerchantCategoryVO;

import java.util.List;

/**
 * 商家分类控制器
 */
@RestController
@RequestMapping("/merchant-categories")
@RequiredArgsConstructor
public class MerchantCategoryController {

    private final MerchantCategoryService categoryService;

    /**
     * 创建商家分类
     */
    @PostMapping
    public Result<MerchantCategoryVO> createCategory(@RequestBody @Valid MerchantCategoryDTO categoryDTO) {
        MerchantCategoryVO categoryVO = categoryService.createCategory(categoryDTO);
        return Result.success(categoryVO);
    }

    /**
     * 更新商家分类
     */
    @PutMapping("/{id}")
    public Result<MerchantCategoryVO> updateCategory(@PathVariable Long id, @RequestBody @Valid MerchantCategoryDTO categoryDTO) {
        categoryDTO.setId(id);
        MerchantCategoryVO categoryVO = categoryService.updateCategory(categoryDTO);
        return Result.success(categoryVO);
    }

    /**
     * 获取商家分类信息
     */
    @GetMapping("/{id}")
    public Result<MerchantCategoryVO> getCategoryById(@PathVariable Long id) {
        MerchantCategoryVO categoryVO = categoryService.getCategoryById(id);
        return Result.success(categoryVO);
    }

    /**
     * 删除商家分类
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteCategory(@PathVariable Long id) {
        boolean result = categoryService.deleteCategory(id);
        return Result.success(result);
    }

    /**
     * 获取所有商家分类
     */
    @GetMapping
    public Result<List<MerchantCategoryVO>> getAllCategories() {
        List<MerchantCategoryVO> categories = categoryService.getAllCategories();
        return Result.success(categories);
    }

    /**
     * 更新商家分类状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateCategoryStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        boolean result = categoryService.updateCategoryStatus(id, status);
        return Result.success(result);
    }
} 